package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageAllocatingDetailActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageAllocatingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageAllocatingOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * 作者：wsy on 2017/12/5 09:44
 * <p>
 * 邮箱：445979770@qq.com
 */

public class AllocatingOutStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageAllocatingOverviewBean.BodyBean.ItemListBean> mAllocatingOutStorageItemList;
    private LayoutInflater inflater;
    private OutStorageAllocatingOverviewActivity.OutStorageAllocatingOverviewListener mOutStorageAllocatingListener;
    private String mMaterielCode, mTransferNumber, mDeliveryNumber, mApplyDepartment, mApplyPerson, mRequirementsDate, mSendFactory, mSendLocation, mDeliveryDate , mDeliveryVehicles , mDeliveryDriver , mDeliveryNote , isShow, mPickingClassify, mUseDepartment, mObjectDescription, mRemark, mIsBuildingProject , mSendFactoryCode , mSendLoctionCode;

    public AllocatingOutStorageItemListAdapter(Context context, OutStorageAllocatingOverviewBean.BodyBean data, List<OutStorageAllocatingOverviewBean.BodyBean.ItemListBean> outStoragePAllocatingItemList, String materielCode, OutStorageAllocatingOverviewActivity.OutStorageAllocatingOverviewListener outStoragAllocatingOverviewListener) {
        mContext = context;
        mTransferNumber = data.getAllocate_code();
        mDeliveryNumber = data.getAllocate_delivery_code();
//        mApplyDepartment = data.getApplyDepartment();
//        mApplyPerson = data.getApplePerson();
//        mRequirementsDate = data.getRequirementsDate();
        mSendFactory = data.getFty_code() + "-" + data.getFty_name();
        mSendLocation = data.getLocation_code() + " - " + data.getLocation_name();
//        mIsFocusBatch = data.getIsFocusBatch();
        if (!TextUtils.isEmpty(mDeliveryNumber)) {
            mDeliveryDate = data.getDelivery_date();
            mDeliveryVehicles = data.getDelivery_vehicle();
            mDeliveryDriver = data.getDelivery_driver();
            mDeliveryNote = data.getRemark();
        }
//        mSendFactoryCode = data.getFty_code();
//        mSendLoctionCode = data.getLocation_code();
        mAllocatingOutStorageItemList = outStoragePAllocatingItemList;
        mOutStorageAllocatingListener = outStoragAllocatingOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mAllocatingOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mAllocatingOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_allocating_item_list_first_item, null);
            TextView transferNumber = (TextView) convertView.findViewById(R.id.transferNumber);
            TextView deliveryNumber = (TextView) convertView.findViewById(R.id.deliveryNumber);
//            TextView applyDepartment = (TextView) convertView.findViewById(R.id.applyDepartment);
//            TextView applyPerson = (TextView) convertView.findViewById(R.id.applyPerson);
//            TextView requirementsDate = (TextView) convertView.findViewById(R.id.requirementsDate);
            TextView receivingFactory = (TextView) convertView.findViewById(R.id.sendFactory);
            TextView receivingLocation = (TextView) convertView.findViewById(R.id.sendLocation);
            TextView deliveryDate = (TextView) convertView.findViewById(R.id.deliveryDate);
            TextView deliveryVehicles = (TextView) convertView.findViewById(R.id.deliveryVehicles);
            TextView deliveryDriver = (TextView) convertView.findViewById(R.id.deliveryDriver);
            TextView deliveryNote = (TextView) convertView.findViewById(R.id.deliveryNote);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            PercentRelativeLayout hideLayout1 = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout1);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            transferNumber.setText(mTransferNumber);
//            applyDepartment.setText(mApplyDepartment);
            deliveryNumber.setText(mDeliveryNumber);
//            applyPerson.setText(mApplyPerson);
//            requirementsDate.setText(mRequirementsDate);
            receivingFactory.setText(mSendFactory);
            receivingLocation.setText(mSendLocation);
            materielCode.setText(mMaterielCode);
            if (!TextUtils.isEmpty(mDeliveryNumber)) {
                hideLayout1.setVisibility(View.VISIBLE);
                deliveryDate.setText(mDeliveryDate);
                deliveryVehicles.setText(mDeliveryVehicles);
                deliveryDriver.setText(mDeliveryDriver);
                deliveryNote.setText(mDeliveryNote);
            }
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mOutStorageAllocatingListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageAllocatingListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            OutStorageAllocatingOverviewBean.BodyBean.ItemListBean PickingOutstorageItem = mAllocatingOutStorageItemList.get(position - 1);
            AllocatingOutStorageItemListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_allocating_item_list, null);
                viewHolder = new AllocatingOutStorageItemListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (AllocatingOutStorageItemListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, PickingOutstorageItem);
        }
        return convertView;
    }

    private void initView(AllocatingOutStorageItemListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.canSendNumber = (TextView) view.findViewById(R.id.canSendNumber);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, AllocatingOutStorageItemListAdapter.ViewHolder viewHolder, View view, OutStorageAllocatingOverviewBean.BodyBean.ItemListBean pallocatingOutStorageItem) {
        viewHolder.serialNumber.setText(pallocatingOutStorageItem.getStock_output_rid());
        viewHolder.materielCode.setText(pallocatingOutStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(pallocatingOutStorageItem.getMat_name());
        viewHolder.canSendNumber.setText(mContext.getResources().getString(R.string.activity_out_storage_allocating_item_list_can_send) + pallocatingOutStorageItem.getLast_qty() + pallocatingOutStorageItem.getUnit_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(pallocatingOutStorageItem.getActualSendNumber()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + pallocatingOutStorageItem.getActualSendNumber() + pallocatingOutStorageItem.getUnit_name()));
        if (TextUtils.isEmpty(pallocatingOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStorageAllocatingDetailActivity.class);
                String serial = viewHolder.serialNumber.getText().toString();
                intent.putExtra(OutStorageExtra.SERIAL, serial);
//                intent.putExtra(OutStorageExtra.SEND_FACTORY_CODE, mSendFactoryCode);
//                intent.putExtra(OutStorageExtra.SEND_LOCATION_CODE, mSendLoctionCode);
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, canSendNumber, outStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
